/**
 * related to perfectInfo.ui
 * 
 * @Author : wzgl5533
 * @Timestamp : 2017-07-21
 */
// 引入组件
var nf = sm("do_Notification");
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var do_Global = sm("do_Global");
var do_Network = sm("do_Network");
var dataCache = sm("do_DataCache");
var dojs = require("dojs");
var myjs = require("myjs/my");
//实例化组件
var root = ui("root");
var nickNameEdt = ui("do_TextField_nick_name");
var addressLy = ui("do_ALayout_edt_ly");
var addressLabel = ui("do_Label_address");
var submitBtn = ui("do_Button_submit");
var skipBtn = ui("do_Button_skip");
//变量
var dynamicButton = null;// 按钮效果
var currentOption = null;
var province = null;
var city = null;
var provinceID = null;
var cityID = null;

// 获取初始化配置数据
dynamicButton = "dynamicButton";
var initData = do_Page.getData();
// 引入标题栏，128px
root.add("topBar", "source://view/common/topbar.ui", 0, 0);
//引入地址选择
var id = root.add("addressSelect", "source://modules/addressselect/src/addressSelect.ui", 0, 0);
//var addressSelect = ui("addressSelect");
do_Page.fire("initAddressSelect",{type:2});
initView();
//初始化按钮效果
dojs.style.css(submitBtn, dynamicButton);
dojs.style.css(skipBtn, dynamicButton);
dojs.style.css(addressLy, dynamicButton);
dojs.page.allowHideKeyboard([submitBtn,skipBtn]);
//添加事件------------------------------------------------------------------
var testJs = require("source://view/perfectinfo/test");
//昵称
testJs.edtFocusEvent(nickNameEdt, ui("do_Label_nick_name_line"));
//地址
dojs.page.onTouch(addressLy,function(){
	//弹出地址选择框
	showAddress();
});
//按钮
dojs.page.onTouch(submitBtn,function(){
	submitInfo();
});
dojs.page.onTouch(skipBtn,function(){
	// 打开首页页面
	var entrance = require("source://script/mymodule/entrance");
	entrance.openHome.openHomePage();
	dojs.core.closePage();
});
//订阅获取最终地址消息
do_Page.on("finalAddress",function(data){
	
	province = data.province;
	city = data.city;
	var address = province == city?province:province+" "+city;
	addressLabel.text = address;
	provinceID = data.provinceID;
	cityID = data.cityID
})

//初始化界面-------------------------------------------------------------------------
function initView() {

	if (dojs.core.isNullData(initData)) {
		dojs.core.toast("初始化数据为空！");
	} else {
		currentOption = initData;
		// 初始化界面
		nickNameEdt.hint = initData.nickName.hint;
		initDefaultAddress();

		// 发消息,初始化标题栏
		do_Page.fire("initTopBarMsg", {
			text : dojs.core.isNullData(initData.title) ? "" : initData.title,
			isBack : initData.allowClose
		});
	}
}
//初始化默认地址------------------------------------------------
function initDefaultAddress() {
	var defaultAddress = do_Global.getMemory(do_Global.getMemory("data_label").location);
	if (defaultAddress.trim().length > 0) {
		addressLabel.text = defaultAddress;
	}else {
		addressLabel.text = "当前无法定位"
	}
	
}
//显示地址选择
function showAddress() {
	
	do_Page.fire("showView");
	
}

//提交信息
function submitInfo() {
	
	if (dojs.core.isNullData(nickNameEdt.text.trim())) {
		dojs.core.toast("昵称不能为空");
		return
	}
	var user = dataCache.loadData(do_Global.getMemory("data_label").user_data);
	if (dojs.core.isNullData(user)) {
		dojs.core.toast("没有用户信息,请跳过这个步骤");
		return;
	}
	//解析
	deviceone.print(JSON.stringify(user));
	user.nickName = nickNameEdt.text.trim();
	user.province = provinceID;
	user.city = cityID;
	deviceone.print(JSON.stringify(user));
	//提交
	testJs.invoke(user,{province: province,city: city});
	
}








//当前页面下，订阅android系统返回键的事件
do_Page.on("back", function() {
	do_App.closePage();
});